home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / RoundButtonFilter.java < prev    next >
Text File  |  1998-09-15  |  5KB  |  163 lines

  1. /*
  2.  * @(#)RoundButtonFilter.java    1.8 98/03/18
  3.  *
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  *
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  *
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30.  
  31. /**
  32.  * An extensible ImageMap applet class.
  33.  * The active areas on the image are controlled by ImageArea classes
  34.  * that can be dynamically loaded over the net.
  35.  *
  36.  * @author     Jim Graham
  37.  * @version     1.8, 03/18/98
  38.  */
  39. class RoundButtonFilter extends ButtonFilter {
  40.     int Xcenter;
  41.     int Ycenter;
  42.     int Yradsq;
  43.     int innerW;
  44.     int innerH;
  45.     int Yrad2sq;
  46.  
  47.     public RoundButtonFilter(boolean press, int p, int b, int w, int h) {
  48.     super(press, p, b, w, h);
  49.     Xcenter = w/2;
  50.     Ycenter = h/2;
  51.     Yradsq = h * h / 4;
  52.     innerW = w - border * 2;
  53.     innerH = h - border * 2;
  54.     Yrad2sq = innerH * innerH / 4;
  55.     }
  56.  
  57.     public boolean inside(int x, int y) {
  58.     int yrel = Math.abs(Ycenter - y);
  59.     int xrel = (int) (Math.sqrt(Yradsq - yrel * yrel) * width / height);
  60.     return (x >= Xcenter - xrel && x < Xcenter + xrel);
  61.     }
  62.  
  63.     public void buttonRanges(int y, int ranges[]) {
  64.     int yrel = Math.abs(Ycenter - y);
  65.     int xrel = (int) (Math.sqrt(Yradsq - yrel * yrel) * width / height);
  66.     ranges[0] = 0;
  67.     ranges[1] = Xcenter - xrel;
  68.     ranges[6] = Xcenter + xrel;
  69.     ranges[7] = width;
  70.     ranges[8] = ranges[9] = y;
  71.     if (y < border) {
  72.         ranges[2] = ranges[3] = ranges[4] = Xcenter;
  73.         ranges[5] = ranges[6];
  74.     } else if (y + border >= height) {
  75.         ranges[2] = ranges[1];
  76.         ranges[3] = ranges[4] = ranges[5] = Xcenter;
  77.     } else {
  78.         int xrel2 = (int) (Math.sqrt(Yrad2sq - yrel * yrel)
  79.                    * innerW / innerH);
  80.         ranges[3] = Xcenter - xrel2;
  81.         ranges[4] = Xcenter + xrel2;
  82.         if (y < Ycenter) {
  83.         ranges[2] = ranges[3];
  84.         ranges[5] = ranges[6];
  85.         } else {
  86.         ranges[2] = ranges[1];
  87.         ranges[5] = ranges[4];
  88.         }
  89.     }
  90.     }
  91.  
  92.     public int filterRGB(int x, int y, int rgb) {
  93.     boolean brighter;
  94.     int percent;
  95.     int i;
  96.     int xrel, yrel;
  97.     int ranges[] = getRanges(y);
  98.     for (i = 0; i < 7; i++) {
  99.         if (x >= ranges[i] && x < ranges[i+1]) {
  100.         break;
  101.         }
  102.     }
  103.     switch (i) {
  104.     default:
  105.     case 0:
  106.     case 6:
  107.         return rgb & 0x00ffffff;
  108.     case 1:
  109.         brighter = !pressed;
  110.         percent = defpercent;
  111.         break;
  112.     case 5:
  113.         brighter = pressed;
  114.         percent = defpercent;
  115.         break;
  116.     case 2:
  117.         yrel = y - Ycenter;
  118.         xrel = Xcenter - x;
  119.         percent = (int) (yrel * defpercent * 2 /
  120.                  Math.sqrt(yrel * yrel + xrel * xrel))
  121.         - defpercent;
  122.         if (!pressed) {
  123.         percent = -percent;
  124.         }
  125.         if (percent == 0) {
  126.         return rgb;
  127.         } else if (percent < 0) {
  128.         percent = -percent;
  129.         brighter = false;
  130.         } else {
  131.         brighter = true;
  132.         }
  133.         break;
  134.     case 4:
  135.         yrel = Ycenter - y;
  136.         xrel = x - Xcenter;
  137.         percent = (int) (yrel * defpercent * 2 /
  138.                  Math.sqrt(yrel * yrel + xrel * xrel))
  139.         - defpercent;
  140.         if (pressed) {
  141.         percent = -percent;
  142.         }
  143.         if (percent == 0) {
  144.         return rgb;
  145.         } else if (percent < 0) {
  146.         percent = -percent;
  147.         brighter = false;
  148.         } else {
  149.         brighter = true;
  150.         }
  151.         break;
  152.     case 3:
  153.         if (!pressed) {
  154.         return rgb & 0x00ffffff;
  155.         }
  156.         brighter = false;
  157.         percent = defpercent;
  158.         break;
  159.     }
  160.     return filterRGB(rgb, brighter, percent);
  161.     }
  162. }
  163.